﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebAppAuthAdmin.AuthApiManagementServiceReference;
using WebAppAuthAdmin.Code;
using Web.Framework;
using AuthApi.Contracts;
using WebAppAuthAdmin.Code.Security;

namespace WebAppAuthAdmin.App
{
    public partial class OperationCreate : BasePage
    {        
        private OperationIncluded operationCurrent
        {
            get
            {
                return (OperationIncluded)ViewState["OperationIncluded"];
            }
            set
            {
                ViewState["OperationIncluded"] = value;
            }
        }

        private List<LinkButton> actionButtons = new List<LinkButton>();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            LinkButton crearContactoButton = new LinkButton();
            crearContactoButton.ID = "guardarButton";
            crearContactoButton.Text = "Guardar";
            crearContactoButton.Click += new EventHandler(lnkGuardar_Click);
            actionButtons.Add(crearContactoButton);

            base.LoadContextActions(actionButtons);
        }

        protected override void ApplySecurity()
        {
            bool hasPermission;
            if (operationCurrent.Id == 0)
            {
                hasPermission = HasPermissionFor(AppSecurity.APP_OPERATION_CREAR);
            }
            else
            {
                hasPermission = HasPermissionFor(AppSecurity.APP_OPERATION_EDITAR);
            }

            actionButtons.Where(a => a.ID == "guardarButton").Single().Visible = hasPermission;
            this.lnkGuardar.Visible = hasPermission;            
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                HasPermissionToSeeMe(AppSecurity.APP_OPERATION_VIEW);

                if (!this.IsPostBack)
                {
                    if (this.Request.QueryString["applicationId"] != null)
                    {
                        operationCurrent = new OperationIncluded();
                        operationCurrent.ApplicationId = Convert.ToInt32(this.Request.QueryString["applicationId"]);                        
                    }
                    else if (this.Request.QueryString["Id"] != null)
                    {
                        operationCurrent = WebServiceHelper.GetOperationById(Convert.ToInt32(this.Request.QueryString["Id"]));
                    }
                    else
                    {
                        ExceptionManager.Instance.ShowWarning("No se proporcionó el paramtro aplicación");
                    }

                    Bind();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void Bind()
        {
            this.txtOperacionName.Text = operationCurrent.Name;
            this.txtOperacionDescription.Text = operationCurrent.Description;
        }

        private void SetData()
        {
            operationCurrent.Name = this.txtOperacionName.Text;
            operationCurrent.Description = this.txtOperacionDescription.Text;
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                SetData();
                operationCurrent = WebServiceHelper.SaveOperation(operationCurrent);
                this.Response.Redirect(string.Format("/App/ApplicationEdit.aspx?Id={0}", operationCurrent.ApplicationId), true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

       
    }
}